import Cookies from "js-cookie";

window.aun = {};
window.aun.lib = {};
window.aun.lib.Cookies = Cookies;
window.aun.components = {};
window.aun.vuecomponents = {};
window.aun.lazycomponents = {};
window.aun.lastcomponent = "";
window.aun.onCreateComponentReady = function() {};
window.aun.createCompoent = function(name, vuecomponent) {
    window.aun.lastcomponent = name;
    window.aun.components[name] = vuecomponent;
    window.aun.vuecomponents[name] = Vue.component(name, vuecomponent);
    window.aun.onCreateComponentReady();
}

window.aun.loadScript = function(url) {
    return new Promise(function(resolve, reject) {
        // Adding the script tag to the head as suggested before
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = url;

        // Then bind the event to the callback function.
        // There are several events for cross browser compatibility.
        script.onreadystatechange = function() {
            resolve();
        };
        script.onload = function() {
            resolve();
        };
        script.onerror = function(e) {
            reject(e);
        }

        // Fire the loading
        head.appendChild(script);
    })
}

window.aun.createApp = function() {
    window.app = new Vue({
        el: '#app',
        data: {
            components: []
        },
        mounted() {
            var self = this;
            window.aun.onAppReady(self);
        }
    }); 
}
